package dimension.starry.block.machine.industry.steamer;

import dimension.starry.StarryDimension;
import dimension.starry.block.machine.industry.IndustryMachineRegister;
import dimension.starry.block.typeInterface.Machine;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import java.util.HashSet;
import java.util.Set;

public class SteamerEntry extends Machine {

    private static final Text MACHINE_BROKEN = Text.translatable(
        StarryDimension.MOD_ID + ".gui.message.machine_not_complete");

    @Override
    public BlockEntity createBlockEntity(BlockPos blockPos, BlockState state) {
        return new SteamTileEntity(blockPos, state);
    }

    @Override
    public ActionResult onUse(
        BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit
    ) {
        if (worldIn.isClient() || hand != Hand.MAIN_HAND) {
            return super.onUse(state, worldIn, pos, player, hand, hit);
        }

        Set<Block> blockSet = new HashSet<>();
        for (int i = -2; i <= 2; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = -2; k <= 2; k++) {
                    blockSet.add(worldIn.getBlockState(pos.add(i, j, k)).getBlock());
                }
            }
        }
        boolean hasBack = blockSet.contains(IndustryMachineRegister.STEAMER_BACK.getBlock());
        boolean hasEntry = blockSet.contains(IndustryMachineRegister.STEAMER_ENTRY.getBlock());
        boolean hasTankFront = blockSet.contains(IndustryMachineRegister.STEAMER_TANK_FRONT.getBlock());
        boolean hasTankBack = blockSet.contains(IndustryMachineRegister.STEAMER_TANK_BACK.getBlock());
        boolean hasFlywheel = blockSet.contains(IndustryMachineRegister.STEAMER_FLYWHEEL.getBlock());
        boolean hasStore = blockSet.contains(IndustryMachineRegister.STEAMER_STORE.getBlock());
        boolean hasBase = blockSet.contains(IndustryMachineRegister.STEAMER_BASE.getBlock());
        if (!hasBack || !hasEntry || !hasTankFront || !hasTankBack || !hasFlywheel || !hasStore || !hasBase) {
            player.sendMessage(MACHINE_BROKEN, false);
            return ActionResult.FAIL;
        }

        return super.onUse(state, worldIn, pos, player, hand, hit);
    }
}
